package cac.fenjian.resource.msaccmat.rest;

import cac.fenjian.resource.MsAccMat.dto.MsAccMatDTO;
import cac.fenjian.resource.MsWpTool.dto.MsWpToolBasicDTO;
import cac.fenjian.resource.msaccmat.service.MsAccMatService;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;

//RestController注解为ResponseBody+Controller结合体，直接返回JSON对象
@RestController
@Api(tags = "resource", description = "employee_basic1")
@RequestMapping("/api/wms/task/api/act/fenjian/resource/MsAccMatRest")

public class MsAccMatRest {

    @Autowired
    private MsAccMatService msAccMatService;

    @PostMapping("/searchByPage")
    public ResponseMsg<QueryRespBean<MsAccMatDTO>> searchMsAccMatByPage(@RequestBody QueryReqBean<MsAccMatDTO>queryReqBean){
        ResponseMsg<QueryRespBean<MsAccMatDTO>> responseMsg = new ResponseMsg<>();
        if(StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())){
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(),queryReqBean.getSord(),MsWpToolBasicDTO.class);
            if(StringUtils.hasText(queryReqBean.getKeyWord())){
                queryReqBean.setSortExp(sordExp);
            }
        }
        if(StringUtils.hasText(queryReqBean.getKeyWord())){
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            MsAccMatDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<MsAccMatDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<MsAccMatDTO> result = msAccMatService.searchMsAccMatByPage(queryReqBean,queryReqBean.getSortExp());
        responseMsg.setResponseBody(result);
        return  responseMsg;
    }


    /**
     * 新增物料数据
     * @param msAccMatDTO
     * @return
     */
    @PostMapping("/insert/v1")
    public ResponseMsg<String> insertMsAccMatDTO(@RequestBody MsAccMatDTO msAccMatDTO){
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msAccMatService.insertMsAccMatDTO(msAccMatDTO));
        return responseMsg;
    }

    /**
     *更改接收状态
     * @param map
     * @return
     */
    @PostMapping("/changeAccType")
    public ResponseMsg<Integer> changeAccType(@RequestBody Map map){
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        String id = (String) map.get("id");
        String type = (String) map.get("type");
        String remark = (String) map.get("remark");
        responseMsg.setResponseBody(msAccMatService.changeAccType(id,type,remark));
        return responseMsg;
    }
}
